package com.tiger.api;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tiger.lbs.Address;
import com.tiger.lbs.LBS;
import com.tiger.web.BaseApi;


/**
 * Place Api
 * 
 * @author andy
 * 
 */
@Controller
@RequestMapping("/api/v1")
public class PlaceApi extends BaseApi {
	
	@Autowired
	private LBS lbs;
	
	@RequestMapping(value = "/places", method = RequestMethod.GET)
	@ResponseBody
	public Object getPlaces(@RequestParam(defaultValue="") String city, String key, @RequestParam(defaultValue="") String attachCities) {
		if(StringUtils.isEmpty(key)) {
			return Collections.emptyList();
		}
		List<Address> places = lbs.queryPlace(key, city);
		if(places == null || places.isEmpty()) {
			return Collections.emptyList();
		}
		List<Address> result = new ArrayList<>();
		for (int i = 0; i < places.size(); i++) {
			Address addr = places.get(i);
			
			if(StringUtils.isBlank(city) && StringUtils.isBlank(attachCities)) {
				result.add(addr);
			} else if(StringUtils.isNotBlank(city) && city.indexOf(addr.getCity()) != -1) {
				result.add(addr);
			} else if(StringUtils.isNotBlank(attachCities) && attachCities.indexOf(addr.getCity()) != -1) {
				result.add(addr);
			}
		}
		return result;
	}
	
}
